let requestUrl;

function addHandler() {
	requestUrl = "/chongwu_taozhuang_category/insert";
	
	$("#taozhuang_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#taozhuang_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/chongwu_taozhuang_category/updateById";
		
		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#skillId").combobox("setValue", rowData.skillId);
		$("#waiguanId").combobox("setValue", rowData.waiguanId);
		$("#chongwuId").combobox("setValue", rowData.chongwuId);

		$("#taozhuang_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#chongwu_id").combobox({
		url: "/chongwu_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-选择宠物-",
		formatter: function(row) {
			return "<img height='18' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#_name_").textbox({
		width: 150,
		prompt: "输入套装名称"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let chongwuId = $("#chongwu_id").combobox("getValue");
		let name = $("#_name_").textbox("getValue");

		$("#taozhuang_list").datagrid("reload", {
			chongwuId: chongwuId,
			name: name
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 150,
		required: true
	});

	$("#skillId").combobox({
		url: "/chongwu_taozhuang_skill_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img src='" + row.image + "' width='18' />&nbsp;" + row.name;
		}
	});

	$("#waiguanId").combobox({
		url: "/chongwu_waiguan_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<span>" + row.name + "（" + row.note + "）</span>";
		}
	});

	$("#chongwuId").combobox({
		url: "/chongwu_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img height='18' src='" + row.image + "' />&nbsp;" + row.name;
		},
		onSelect: function(record) {
			$("#name").textbox("setValue", record.name + "专属");
		}
	});

	$("#taozhuang_dialog").dialog({
		title: "宠物套装",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#taozhuang_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#taozhuang_dialog").dialog("close");
						$("#taozhuang_list").datagrid("reload");
					}, error);
				});
			}
		},{
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#taozhuang_dialog").dialog("close");
				$("#taozhuang_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#taozhuang_list").datagrid({
		url: "/chongwu_taozhuang_category/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-print",
			text: "打印",
			handler: function() {
				$("#taozhuang_list").datagrid("print", "DataGrid");
			}
		}, "-", {
			iconCls: "icon-save",
			text: "导出为excell",
			handler: function() {
				$("#taozhuang_list").datagrid("toExcel", "宠物装备套装.xls");
			}
		}],
		columns: [[
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "chongwuId", title: "宠物类型", align: "center", width: 40
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/chongwu_category/selectById", {
							id: value
						}, function(resp) {
							let data = resp.data;

							$("#chongwu_" + rowIndex).attr({
								"alt": data.name,
								"src": data.image,
								"title": data.name
							});
						}, error);

						return "<img height='" + size + "' id='chongwu_" + rowIndex + "' />";
					}

					return "/";
				}
			},
			{field: "name", title: "套装名称", align:"center", width: 50},
			{field: "skillId", title: "套装技能", align: "center", width: 40
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/chongwu_taozhuang_skill_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#skill_" + rowIndex).attr({
							"alt": data.name,
							"src": data.image,
							"title": data.name
						});
					}, error);

					return "<img height='" + size + "' id='skill_" + rowIndex + "' />";
				}
			},
			{field: "waiguanId", title: "宠物外观", align: "center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/chongwu_waiguan_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#waiguan_" + rowIndex).html(data.name);
					}, error);
					
					return "<div id='waiguan_" + rowIndex + "'></div>";
				}
			}
		]]
	});

});